package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.constant.DBConstants;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.entity.OrderBase;
import com.bto.solar.work.service.BtoCompanyService;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.service.BtoUserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王小波
 * @description:
 * @date 2023/7/21 18:04
 */
@Component
public class JieSuanStageHandler implements StageHandler {
    @Autowired
    private IOrderBaseService orderService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoCompanyService btoCompanyService;
    @Autowired
    private BtoUserService btoUserService;
    @Autowired
    private GSContractStageHandler gsContractStageHandler;


    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        //查询当前的用户公司类型：直营、代理商
        OrderBase orderBase = orderService.getById(orderId);
        val company = btoCompanyService.getCompanyById(orderBase.getCompanyId());
        String companyType = company.getType();

        //代理商-创建并网结算
        if (DBConstants.COMPANY_TYPE_AGENT.equals(companyType)) {
            btoOrderStateService.createOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), BizTaskEnum.TASK_BWJSXX.getTaskId(),
                    BizTaskStateEnum.WAITING_FILLED.getStateId());
        }
        //直营-创建结算
        else {
            btoOrderStateService.createOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), null,
                    BizTaskStateEnum.WAITING_FILLED.getStateId());
        }
    }


    /**
     * 直营 -- 结算金额 -- null
     * 代理商 -- 并网结算信息博通审核 -- TASK_BWJSXX
     * 代理商 -- 资料结算信息博通审核 -- TASK_ZLJSXX
     */
    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        //查询当前的用户公司类型：直营、代理商
        OrderBase orderBase = orderService.getById(orderId);
        val company = btoCompanyService.getCompanyById(orderBase.getCompanyId());
        String companyType = company.getType();

        //代理商 - 并网结算、资料结算 （TASK_BWJSXX， TASK_ZLJSXX） --》 博通待审核 （前置条件：待填写、博通拒绝）
        if (DBConstants.COMPANY_TYPE_AGENT.equals(companyType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), taskId,
                    BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(),
                    BizTaskStateEnum.WAITING_FILLED.getStateId(),
                    BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId());
        }
        //非代理商
        else {
            gsContractStageHandler.createNewInitState(orderId, BizStageEnum.GS_CONTRACT.getStageId(), BizTaskEnum.TASK_GSDHTXX.getTaskId());
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), taskId,
                    BizTaskStateEnum.FILLED_OK.getStateId(),
                    BizTaskStateEnum.WAITING_FILLED.getStateId());
        }
    }


    /**
     * 直营 -- 购售电合同博通通过 --》 推给越秀（越秀待审核）--》 订单完成  -- null
     * 代理商 -- 并网结算信息博通通过 --》 开启购售电合同任务（购售电合同待填写）
     * 代理商 -- 资料结算信息博通通过 --》 订单完成
     */
    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId, String approvalType) {
        //查询当前的用户公司类型：直营、代理商
        OrderBase orderBase = orderService.getById(orderId);
        val company = btoCompanyService.getCompanyById(orderBase.getCompanyId());
        String companyType = company.getType();
        //博通审核通过
        //代理商
        if (DBConstants.COMPANY_TYPE_AGENT.equals(companyType)) {
            if (BizTaskEnum.TASK_BWJSXX.getTaskId().equals(taskId)) {
                //开启购售电合同任务（购售电合同待填写）
                gsContractStageHandler.createNewInitState(orderId, BizStageEnum.GS_CONTRACT.getStageId(), BizTaskEnum.TASK_GSDHTXX.getTaskId());
            }

            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), taskId,
                    BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);

            //资料结算信息博通通过 --》 订单完成
            if (BizTaskEnum.TASK_ZLJSXX.getTaskId().equals(taskId)) {
                //订单完成
                updateOrderFinishedState(orderId);
            }
        }

    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId, String approvalType, Object rejectInfo) {
        //博通拒绝 并网结算、资料结算
        if (Constants.APPROVAL_BTO.equals(approvalType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), taskId,
                    BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);
        }
        //越秀拒绝购售电合同
        else if (Constants.APPROVAL_LEASE.equals(approvalType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(), taskId,
                    BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(), true);
        }

    }

    private void updateOrderFinishedState(String orderId) {
        btoOrderStateService.createOrderState(orderId, BizStageEnum.FINISHED.getStageId(), null, BizTaskStateEnum.FINISHED.getStateId());
    }


}
